using Microsoft.Phone.Controls.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Controls;
namespace Microsoft.Phone.Controls
{
	public class LengthConverter : TypeConverter
	{
		private static Dictionary<string, double> UnitToPixelConversions;
		public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType)
		{
			switch (Type.GetTypeCode(sourceType))
			{
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
			case 18:
				return true;
			case 16:
			case 17:
				return false;
			}
			return false;
		}
		public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
		{
			if (source == null)
			{
				string text = string.Format(CultureInfo.get_CurrentCulture(), Resources.TypeConverters_ConvertFrom_CannotConvertFromType, new object[]
				{
					base.GetType().get_Name(),
					"null"
				});
				throw new NotSupportedException(text);
			}
			string text2 = source as string;
			if (text2 == null)
			{
				return Convert.ToDouble(source, cultureInfo);
			}
			if (string.Compare(text2, "Auto", 5) == 0)
			{
				return double.NaN;
			}
			string text3 = text2;
			double num = 1.0;
			using (Dictionary<string, double>.Enumerator enumerator = LengthConverter.UnitToPixelConversions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, double> current = enumerator.get_Current();
					if (text3.EndsWith(current.get_Key(), 4))
					{
						num = current.get_Value();
						text3 = text2.Substring(0, text3.get_Length() - current.get_Key().get_Length());
						break;
					}
				}
			}
			object result;
			try
			{
				result = num * Convert.ToDouble(text3, cultureInfo);
			}
			catch (FormatException)
			{
				string text4 = string.Format(CultureInfo.get_CurrentCulture(), Resources.TypeConverters_Convert_CannotConvert, new object[]
				{
					base.GetType().get_Name(),
					text2,
					typeof(double).get_Name()
				});
				throw new FormatException(text4);
			}
			return result;
		}
		public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
		{
			return TypeConverters.CanConvertTo<double>(destinationType);
		}
		public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
		{
			if (value is double)
			{
				double num = (double)value;
				if (destinationType == typeof(string))
				{
					if (!num.IsNaN())
					{
						return Convert.ToString(num, cultureInfo);
					}
					return "Auto";
				}
			}
			return TypeConverters.ConvertTo(this, value, destinationType);
		}
		static LengthConverter()
		{
			// Note: this type is marked as 'beforefieldinit'.
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			dictionary.Add("px", 1.0);
			dictionary.Add("in", 96.0);
			dictionary.Add("cm", 37.795275590551178);
			dictionary.Add("pt", 1.3333333333333333);
			LengthConverter.UnitToPixelConversions = dictionary;
		}
	}
}
